package de.slothsoft.wasbinich.swingclient;

import java.util.prefs.Preferences;

import javax.swing.JOptionPane;

import de.slothsoft.wasbinich.core.WasBinIch;
import de.slothsoft.wasbinich.core.util.ExceptionUtil;
import de.slothsoft.wasbinich.swingclient.control.MainPanel;
import de.slothsoft.wasbinich.swingclient.res.Messages;

public final class WasBinIchSwingClient {

    public static final String APPLICATION_VERSION = "1.1.0";
    public static final Preferences PREFERENCES = Preferences.userNodeForPackage(WasBinIchSwingClient.class);

    private static MainPanel mainPanel;

    public static MainPanel getMainPanel() {
	return mainPanel;
    }

    static void setMainPanel(MainPanel mainPanel) {
	WasBinIchSwingClient.mainPanel = mainPanel;
    }

    public static void handleFatalException(Exception exception) {
	handleException(exception);
	if (mainPanel != null) {
	    mainPanel.switchScreen(MainPanel.SCREEN_TITLE);
	}
    }

    public static void handleException(Exception exception) {
	if (ExceptionUtil.isCancelledByUserException(exception)) {
	    if (WasBinIch.DEBUG) {
		System.err.println("The action was cancelled by the user!");
	    }
	} else {
	    if (WasBinIch.DEBUG) {
		exception.printStackTrace();
	    }
	    String message = ExceptionUtil.getErrorMessage(exception);
	    JOptionPane.showMessageDialog(mainPanel, message, Messages.getString("WasBinIchSwingClient.error.title"),
		    JOptionPane.ERROR_MESSAGE);
	}
    }

    private WasBinIchSwingClient() {
	// hide me
    }

}
